/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.actions.modelo;

import com.mapping.Marca;
import com.mapping.TipoDisp;
import com.opensymphony.xwork2.ActionSupport;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.EntityTransaction;
import javax.persistence.Persistence;
import javax.persistence.TypedQuery;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.Result;

/**
 *
 * @author PARDmx
 */
@Namespace(value = "/")
@Action(value = "altaModeloPreP", results = {
    @Result(name = "success", location = "/GestionModelos/gestionAgregarModelo.jsp")})
public class AltaModeloPre extends ActionSupport{

    private Marca[] marcas;
    private TipoDisp[] tipos;

    @Override
    public String execute() {

        EntityManagerFactory emf = Persistence.createEntityManagerFactory("inventarios");
        EntityManager em = emf.createEntityManager();
        EntityTransaction tx = em.getTransaction();
        tx.begin();

        TypedQuery e = em.createQuery("SELECT m FROM Marca m", Marca.class);
        List<Marca> resultList = e.getResultList();
        marcas = new Marca[resultList.size()];

        for (int i = 0; i < resultList.size(); i++) {
            marcas[i] = resultList.get(i);
        }
        
        TypedQuery e2 = em.createQuery("SELECT t FROM TipoDisp t", TipoDisp.class);
        List<TipoDisp> resultList2 = e2.getResultList();
        tipos = new TipoDisp[resultList2.size()];

        for (int i = 0; i < resultList2.size(); i++) {
            tipos[i] = resultList2.get(i);
        }        

        em.flush();
        tx.commit();
        em.close();
        return SUCCESS;
    }

    public Marca[] getMarcas() {
        return marcas;
    }

    public TipoDisp[] getTipos() {
        return tipos;
    }
    
    
}
